package com.somsoft.handjc.activity.query.xzsp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.somsoft.handjc.activity.MainActivity;
import com.somsoft.handjc.activity.R;
import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.Utils;
import com.somsoft.handjc.util.XzspDataUtil;
/**
 * 审批  综合时限查询
 * 
 * 
 */
@SuppressWarnings("all")
public class XzspSxQueryListActivity extends ListActivity {

	private SimpleAdapter listItemAdapter;  // ListView的适配器
	private List<Map<String, Object>> listItem;  // ListView的数据源，这里是一个HashMap的列表

	private int totalPage = 0;
	private int pageNum = 1;
	
	private TextView tv = null;
	
	private String pageInfo = "";
	/**
	 * 设置布局显示为目标有多大就多大
	 */
	private LayoutParams WClayoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.WRAP_CONTENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);
	/**
	 * 设置布局显示目标最大化
	 */
	private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.FILL_PARENT,
			LinearLayout.LayoutParams.FILL_PARENT);

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		
		
		setTitle("行政审批[综合时限查询]");
		boolean error = false;
		try {
			listItem = getData(1,getIntent());
		} catch (Exception e) {
			error = true;
			listItem = new ArrayList<Map<String,Object>>();
			Toast.makeText(XzspSxQueryListActivity.this, "加载数据失败！", Toast.LENGTH_SHORT).show();
		}
		LinearLayout layoutPageInfo = new LinearLayout(this);
		layoutPageInfo.setOrientation(LinearLayout.HORIZONTAL);
		
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.HORIZONTAL);

		Button btnFirst = new Button(this);
		btnFirst.setText("首页");
		btnFirst.setWidth(130);
		btnFirst.setGravity(Gravity.CENTER);
		
		
		Button btnPre = new Button(this);
		btnPre.setText("上一页");
		btnPre.setWidth(130);
		btnPre.setGravity(Gravity.CENTER);
		
		Button btnNext = new Button(this);
		btnNext.setText("下一页");
		btnNext.setWidth(130);
		btnNext.setGravity(Gravity.CENTER);
		
		Button btnEnd = new Button(this);
		btnEnd.setText("尾页");
		btnEnd.setWidth(130);
		btnEnd.setGravity(Gravity.CENTER);

		tv = new TextView(this);
		tv.setText(pageInfo);
		tv.setTextSize(20);
		tv.setGravity(Gravity.CENTER_VERTICAL);
		
		
		layoutPageInfo.addView(tv, FFlayoutParams);
		layout.addView(btnFirst, FFlayoutParams);
		layout.addView(btnPre, FFlayoutParams);
		layout.addView(btnNext, FFlayoutParams);
		layout.addView(btnEnd, FFlayoutParams);
		
		layout.setGravity(Gravity.CENTER);
		LinearLayout loadingLayout = new LinearLayout(this);
		loadingLayout.setOrientation(LinearLayout.VERTICAL);
		loadingLayout.addView(layoutPageInfo, WClayoutParams);
		loadingLayout.addView(layout, WClayoutParams);
		loadingLayout.setGravity(Gravity.CENTER);

		ListView listView = getListView();

		listView.addFooterView(loadingLayout);

		btnFirst.setOnClickListener(new MyOnClickListener() );
		btnPre.setOnClickListener(new MyOnClickListener() );
		btnNext.setOnClickListener(new MyOnClickListener() );
		btnEnd.setOnClickListener(new MyOnClickListener() );
		
		listItemAdapter = new SimpleAdapter(this, listItem, R.layout.common_list_with_1image, 
				new String[] {"YWLSH", "commonDetail", "resultTxt","resultImg" },
				new int[] { R.id.YWLSH, R.id.commonDetail, R.id.resultTxt,R.id.resultImg});
		setListAdapter(listItemAdapter);
		Utils.closeLoading();
	}

	
	private List<Map<String, Object>> getData(int pageNum,Intent it) throws Exception{
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("pageNum",pageNum+""));
		params.add(new BasicNameValuePair("method","query"));
		if(it!=null){
			String yxtywlsh = it.getStringExtra("yxtywlsh");
			String projectName = it.getStringExtra("projectName");
			String deptName = it.getStringExtra("deptName");
			
			if(deptName!=null && !"".equals(deptName)){
				params.add(new BasicNameValuePair("deptName",deptName));
			}
			
			if(yxtywlsh!=null && !"".equals(yxtywlsh)){
				params.add(new BasicNameValuePair("yxtywlsh",yxtywlsh));
			}
			if(projectName!=null && !"".equals(projectName)){
				params.add(new BasicNameValuePair("xmmc",projectName));
			}
		}
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			JSONArray array = XzspDataUtil.sxjcList(params);
			Map<String, Object> map = null;
			
			JSONObject pgInfo = array.getJSONObject(0);
			totalPage = pgInfo.getInt("totalPage");
			pageInfo = pgInfo.getString("pageInfo");
			
			for(int i = 1; i < array.length(); i++){
				JSONObject obj = array.getJSONObject(i);
				map = new HashMap<String, Object>(); 
				map.put("YWLSH", obj.getString("YWLSH"));
				
				String content = "";
				content += "时限监察类型："+obj.getString("LIMITTYPE")+"\n";
				content += "审批事项名称："+obj.getString("SPSXMC")+"\n";
				content += "项目名称："+obj.getString("XMMC")+"\n";
				content += "业务流水号："+obj.getString("YXTYWLSH")+"\n";
				content += "受理时间："+obj.getString("SHOULISJ")+"\n";
				content += "所剩天数："+obj.getString("LEAVINGSDAY")+"个工作日\n";
				content += "办理状态："+obj.getString("BLZTMC");
						
				map.put("commonDetail", content);
				map.put("resultTxt", "时限监察结果：");
				
				String result = obj.getString("CURSTATUSTYPE");//监察结果类型(0:正常;1:预警;2黄牌;5:红牌)
				if("1".equals(result)){
					map.put("resultImg", R.drawable.bell);
				}else if("2".equals(result)){
					map.put("resultImg", R.drawable.yellow);
				}else if("5".equals(result)){
					map.put("resultImg", R.drawable.red);
				}else {
					map.put("resultImg", R.drawable.normal);
				}
				if(tv!=null){
					tv.setText(pageInfo);
				}
				list.add(map);
			}
		} catch (Exception e) {
			throw e;
		}
		return list;

	}
	
	class MyOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			try {
				listItem.clear();
				Button btn = (Button)v;
				
				Intent it = XzspSxQueryListActivity.this.getIntent();
				
				if("首页".equals(btn.getText())){
					listItem.addAll(getData(1,it));
				}else if("上一页".equals(btn.getText())){
					pageNum --;
					if(pageNum<1){
						pageNum = 1;
					}
					listItem.addAll(getData(pageNum,it));
				}else if("下一页".equals(btn.getText())){
					pageNum++;
					if(pageNum>totalPage){
						pageNum = totalPage;
					}
					listItem.addAll(getData(pageNum,it));
				}else if("尾页".equals(btn.getText())){
					listItem.addAll(getData(totalPage,it));
				}
			} catch (Exception e) {
				Toast.makeText(XzspSxQueryListActivity.this, "加载数据失败！", Toast.LENGTH_SHORT).show();
			}
			listItemAdapter.notifyDataSetChanged();
		}
		
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
		String ywlsh = map.get("YWLSH").toString();
		
		Intent it = new Intent(XzspSxQueryListActivity.this,XzspDetailActivity.class);
		it.putExtra("ywlsh", ywlsh);
		startActivity(it);
	} 
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {		
		menu.add(Menu.NONE, 10, 10, "首页").setIcon(android.R.drawable.ic_menu_compass);
		menu.add(Menu.NONE, 20, 20, "查询").setIcon(android.R.drawable.ic_menu_search);
		menu.add(Menu.NONE, 90, 90, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;											
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {		// 选中某个菜单项
		Intent it = null;
		switch (item.getItemId()) {								// 判断菜单项ID
		case  10:
			it = new Intent(XzspSxQueryListActivity.this,MainActivity.class);
			startActivity(it);
			break;
		case  20:
			it = new Intent(XzspSxQueryListActivity.this,XzspQueryActivity.class);
			it.putExtra("from", "sxcx");
			startActivity(it);
			break;
		case  90:
			MyApplication.getInstance().exit();
			break;
		}
		return false;
	}
}
